﻿using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.Windows;
using Autodesk.Windows;
using System;
using System.Windows.Media.Imaging;

namespace DonetArxLib.EmployeeSample
{
    public class EmployeeExtApp //: IExtensionApplication // 暂时停止这个app
    {
        #region 成员

        public static string sDivisionDefault = "Sales";
        public static string sDivisionManager = "Fiona Q. Farnsby";

        private ContextMenuExtension _menu = null;
        private PaletteSet _ps = null;

        #endregion

        #region 接口实现

        public void Initialize()
        {
            Autodesk.AutoCAD.ApplicationServices.Core.Application.DocumentManager.MdiActiveDocument?.Editor.WriteMessage("xs app loaded!");
            AddContextMenu();
            //CreatePalette();
#if !_CAD2020
            CreateRibbonUI();
#endif
        }

        public void Terminate()
        {
            Autodesk.AutoCAD.ApplicationServices.Core.Application.DocumentManager.MdiActiveDocument?.Editor.WriteMessage("xs app unload!");
            RemoveContextMenu();
        }

        #endregion

        #region 右键菜单

        private void AddContextMenu()
        {
            try
            {
                _menu = new ContextMenuExtension();
                _menu.Title = "Acme Employee Menu";

                var mi = new MenuItem("Create Employee");
                mi.Click += Mi_Click;
                _menu.MenuItems.Add(mi);

                Application.AddDefaultContextMenuExtension(_menu);
            }
            catch
            {
            }
        }

        private void RemoveContextMenu()
        {
            try
            {
                if (_menu != null)
                {
                    Application.RemoveDefaultContextMenuExtension(_menu);
                    _menu = null;
                }
            }
            catch
            {
            }
        }

        private void Mi_Click(object sender, EventArgs e)
        {
            using (Autodesk.AutoCAD.ApplicationServices.Core.Application.DocumentManager.MdiActiveDocument.LockDocument())
            {
                new EmployeeCommand().CreateEmployee();
            }
        }

        #endregion

        #region 浮动窗口

        [CommandMethod("xsPalette")]
        private void CreatePalette()
        {
            _ps = new PaletteSet("Test Palette Set");
            _ps.MinimumSize = new System.Drawing.Size(300, 300);
            var ctrl = new EmployeeDetailCtrl();
            _ps.Add("test", ctrl);
            _ps.Visible = true;
            _ps.Dock = DockSides.Left;

        }

        #endregion

        #region Ribbon菜单

#if !_CAD2020
        private void CreateRibbonUI()
        {
            var panel = ComponentManager.Ribbon
                .AddTab("MyTab", "xs_tab", true)
                .AddPanel("MyPanel");
        }

        private RibbonButton CreateRibbonButton()
        {
            var btn = new RibbonButton();
            btn.Name = "linebtn";
            btn.Id = "ID.LINE.BTN";
            btn.AllowInStatusBar = true;
            btn.AllowInToolBar = true;
            btn.GroupLocation = Autodesk.Private.Windows.RibbonItemGroupLocation.Middle;
            btn.IsEnabled = true;
            btn.IsToolTipEnabled = true;

            return null;



            //lineBtn.SetImg(CurPaht.curPaht + "Images\\Line.PNG");//设置按钮图片
            //添加提示对象
            //RibbonToolTip toolTip = new RibbonToolTip();
            //toolTip.Title = "直线";
            //toolTip.Content = "创建直线段";
            //toolTip.Command = "LINE";
            //toolTip.ExpandedContent = "是用LINE命令，可以创建一些列连续的直线段。每条线段都是可以单独进行编辑的直线对象。";
            //string imgToolTipFileName = CurPaht.curPaht + "Images\\LineTooTip.PNG";
            //Uri toolTipUri = new Uri(imgToolTipFileName);
            //BitmapImage toolTipBitmapImge = new BitmapImage(toolTipUri);
            //toolTip.ExpandedImage = toolTipBitmapImge;
            //lineBtn.ToolTip = toolTip;
            ////鼠标进入时的图片
            //lineBtn.ImgHoverFileName = CurPaht.curPaht + "Images\\LineHover.PNG";
            //return lineBtn;
        }
#endif

        #endregion
    }
}
